package com.party.mobile.biz.analyze;

import com.party.common.constant.Constant;
import com.party.common.utils.DateUtils;
import com.party.core.model.distributor.DistributorDetail;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.model.member.MemberAct;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.distributor.IDistributorDetailService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.core.service.member.IMemberActService;
import com.party.core.service.member.IMemberCopartnerCountService;
import com.party.core.service.member.IMemberCopartnerDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 合伙人数据统计服务接口
 * Created by wei.li.
 *
 * @date 2018/1/17 0017
 * @time 18:07
 */
@Service
public class MemberCopartnerDetailBizService {

    @Autowired
    private IMemberCopartnerCountService memberCopartnerCountService;

    @Autowired
    private IMemberCopartnerDetailService memberCopartnerDetailService;

    @Autowired
    private IDistributorDetailService distributorDetailService;

    @Autowired
    private IDistributorRelationService distributorRelationService;

    @Autowired
    private IDistributorCountService distributorCountService;

    @Autowired
    private IMemberActService memberActService;

    /**
     * 更新订单数和金额数
     *
     * @param disRelation 分销关系
     * @param payment     支付数
     */
    public void updateOrderAnalyze(DistributorRelation disRelation, Float payment) {
        String distributorId = disRelation.getDistributorId();
        memberCopartnerCountService.updateOrderAnalyze(payment, distributorId);
        memberCopartnerDetailService.updateOrderAnalyze(payment, distributorId);
    }

    /**
     * 分销 成功调用 更新订单数和金额数和成功数
     *
     * @param disRelation 分销关系
     * @param payment     支付数
     */
    public void updateCrowdfundSuccess(DistributorRelation disRelation, Float payment) {
        String distributorId = disRelation.getDistributorId();
        memberCopartnerCountService.updateCrowdfundSuccess(payment, distributorId);
        memberCopartnerDetailService.updateCrowdfundSuccess(payment, distributorId);
    }

    /**
     * 更新订单数和金额数
     *
     * @param projectId 众筹编号
     * @param payment   支付数
     */
    public void updateOrderAnalyze(String projectId, Float payment) {
        DistributorDetail distributorDetail = distributorDetailService.findByTargetId(projectId);
        if (null != distributorDetail) {
            DistributorRelation distributorRelation = distributorRelationService.get(distributorDetail.getDistributorRelationId());
            if (null != distributorRelation
                    && (DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode().equals(distributorRelation.getType()))
                    || DistributorRelationType.COURSE_DISTRIBUTION.getCode().equals(distributorRelation.getType())) {
                String distributorId = distributorRelation.getDistributorId();

                memberCopartnerCountService.updateOrderAnalyze(payment, distributorId);

                memberCopartnerDetailService.updateOrderAnalyze(payment, distributorId);
            }
        }
    }

    /**
     * 更新报名数
     *
     * @param projectId 众筹编号
     */
    public void updateApplyNum(String projectId) {
        DistributorDetail distributorDetail = distributorDetailService.findByTargetId(projectId);
        if (null != distributorDetail) {
            DistributorRelation distributorRelation = distributorRelationService.get(distributorDetail.getDistributorRelationId());
            if (null != distributorRelation
                    && (DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode().equals(distributorRelation.getType()))
                    || DistributorRelationType.COURSE_DISTRIBUTION.getCode().equals(distributorRelation.getType())) {
                String distributorId = distributorRelation.getDistributorId();

                memberCopartnerCountService.updateApplyNum(projectId, distributorId);

                memberCopartnerDetailService.updateApplyNum(projectId, distributorId);
            }
        }
    }

    /**
     * 更新报名数
     *
     * @param relationId 分销关系id
     * @param targetId   目标编号
     */
    @Transactional
    public void updateApplyNumSub(String relationId, String targetId) {
        DistributorRelation distributorRelation = distributorRelationService.get(relationId);
        if (null != distributorRelation && (
                DistributorRelationType.COURSE_DISTRIBUTION.getCode().equals(distributorRelation.getType())
                        || DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode().equals(distributorRelation.getType()))) {
            MemberAct memberAct = memberActService.get(targetId);

            String formatDate = DateUtils.formatDate(memberAct.getCreateDate(), DateUtils.DATE_PATTERN);
            String formatToday = DateUtils.formatDate(new Date(), DateUtils.DATE_PATTERN);
            String option = formatToday.equals(formatDate) ? Constant.OPTION_ADD : Constant.OPTION_SUB;

            memberCopartnerDetailService.updateApplyRefund(distributorRelation.getDistributorId(), 0d, option);
            memberCopartnerCountService.updateApplyRefund(distributorRelation.getDistributorId(), 0d);
        }
    }

    /**
     * 更新众筹成功数
     *
     * @param projectId 众筹编号
     */
    public void updateSuccess(String projectId) {
        DistributorDetail distributorDetail = distributorDetailService.findByTargetId(projectId);
        if (null != distributorDetail) {
            DistributorRelation distributorRelation = distributorRelationService.get(distributorDetail.getDistributorRelationId());
            if (null != distributorRelation
                    && (DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode().equals(distributorRelation.getType()))
                    || DistributorRelationType.COURSE_DISTRIBUTION.getCode().equals(distributorRelation.getType())) {
                String distributorId = distributorRelation.getDistributorId();

                memberCopartnerCountService.updateSuccess(projectId, distributorId);

                memberCopartnerDetailService.updateSuccess(projectId, distributorId);
            }
        }
    }

    public void updateBrokerage(String memberId, Double brokerage, Boolean isContain, boolean isBegin, double withdrawalTotal) {
        memberCopartnerCountService.updateBrokerage(memberId, brokerage, isContain, isBegin, withdrawalTotal);

        memberCopartnerDetailService.updateBrokerage(memberId, brokerage, isContain, isBegin, withdrawalTotal);
    }
}
